{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import requests   # 项目  关于Github社区python项目热度的数据分析小实践  转载自 https://github.com/shawshany/pygal-webapi"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "url =  'https://api.github.com/search/repositories?q=language:python&sort=stars'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Status code: 200\n"
     ]
    }
   ],
   "source": [
    "r = requests.get(url)\n",
    "print(\"Status code:\",r.status_code)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "dict_keys(['total_count', 'incomplete_results', 'items'])\n"
     ]
    }
   ],
   "source": [
    "response_dict = r.json()\n",
    "print(response_dict.keys())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Total repositories: 2814128\n"
     ]
    }
   ],
   "source": [
    "print(\"Total repositories:\", response_dict['total_count']) "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Repositories returned: 30\n"
     ]
    }
   ],
   "source": [
    "# Explore information about the repositories.\n",
    "repo_dicts = response_dict['items']\n",
    "print(\"Repositories returned:\",len(repo_dicts))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "Keys: 73\n",
      "archive_url\n",
      "archived\n",
      "assignees_url\n",
      "blobs_url\n",
      "branches_url\n",
      "clone_url\n",
      "collaborators_url\n",
      "comments_url\n",
      "commits_url\n",
      "compare_url\n",
      "contents_url\n",
      "contributors_url\n",
      "created_at\n",
      "default_branch\n",
      "deployments_url\n",
      "description\n",
      "downloads_url\n",
      "events_url\n",
      "fork\n",
      "forks\n",
      "forks_count\n",
      "forks_url\n",
      "full_name\n",
      "git_commits_url\n",
      "git_refs_url\n",
      "git_tags_url\n",
      "git_url\n",
      "has_downloads\n",
      "has_issues\n",
      "has_pages\n",
      "has_projects\n",
      "has_wiki\n",
      "homepage\n",
      "hooks_url\n",
      "html_url\n",
      "id\n",
      "issue_comment_url\n",
      "issue_events_url\n",
      "issues_url\n",
      "keys_url\n",
      "labels_url\n",
      "language\n",
      "languages_url\n",
      "license\n",
      "merges_url\n",
      "milestones_url\n",
      "mirror_url\n",
      "name\n",
      "node_id\n",
      "notifications_url\n",
      "open_issues\n",
      "open_issues_count\n",
      "owner\n",
      "private\n",
      "pulls_url\n",
      "pushed_at\n",
      "releases_url\n",
      "score\n",
      "size\n",
      "ssh_url\n",
      "stargazers_count\n",
      "stargazers_url\n",
      "statuses_url\n",
      "subscribers_url\n",
      "subscription_url\n",
      "svn_url\n",
      "tags_url\n",
      "teams_url\n",
      "trees_url\n",
      "updated_at\n",
      "url\n",
      "watchers\n",
      "watchers_count\n"
     ]
    }
   ],
   "source": [
    "repo_dict = repo_dicts[0]\n",
    "print(\"\\nKeys:\",len(repo_dict))\n",
    "for key in sorted(repo_dict.keys()):\n",
    "    print(key)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "Selected information about first repository:\n",
      "Name: awesome-python\n",
      "Owner: vinta\n",
      "Stars: 52577\n",
      "Repository: https://github.com/vinta/awesome-python\n",
      "Created: 2014-06-27T21:00:06Z\n",
      "Updated: 2018-07-16T13:21:56Z\n",
      "Description: A curated list of awesome Python frameworks, libraries, software and resources\n"
     ]
    }
   ],
   "source": [
    "print(\"\\nSelected information about first repository:\")\n",
    "print('Name:',repo_dict['name'])\n",
    "print('Owner:',repo_dict['owner']['login'])\n",
    "print('Stars:',repo_dict['stargazers_count'])\n",
    "print('Repository:',repo_dict['html_url'])\n",
    "print('Created:',repo_dict['created_at'])\n",
    "print('Updated:',repo_dict['updated_at'])\n",
    "print('Description:',repo_dict['description'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "%%cmd\n",
    "pip install pygal"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pygal\n",
    "from pygal.style import LightColorizedStyle as LCS, LightenStyle as LS\n",
    "names, plot_dicts = [], []\n",
    "for repo_dict in repo_dicts:\n",
    "    names.append(repo_dict['name'])\n",
    "\n",
    "    # Get the project description, if one is available.\n",
    "    description = repo_dict['description']\n",
    "    if not description:\n",
    "        description = \"No description provided.\"\n",
    "\n",
    "    plot_dict = {\n",
    "        'value': repo_dict['stargazers_count'],\n",
    "        'label': description,\n",
    "        'xlink': repo_dict['html_url'],\n",
    "        }\n",
    "    plot_dicts.append(plot_dict)\n",
    "# Make visualization.\n",
    "my_style = LS('#333366', base_style=LCS)\n",
    "my_style.title_font_size = 24\n",
    "my_style.label_font_size = 14\n",
    "my_style.major_label_font_size = 18\n",
    "\n",
    "my_config = pygal.Config()\n",
    "my_config.x_label_rotation = 45\n",
    "my_config.show_legend = False\n",
    "my_config.truncate_label = 15\n",
    "my_config.show_y_guides = False\n",
    "my_config.width = 1000\n",
    "\n",
    "chart = pygal.Bar(my_config, style=my_style)\n",
    "chart.title = 'Most-Starred Python Projects on GitHub'\n",
    "chart.x_labels = names\n",
    "\n",
    "chart.add('', plot_dicts)\n",
    "chart.render_to_file('python_repos.svg')# svg格式文件基于XML需要用浏览器打开"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
